package club.caihappy.test;

import club.caihappy.config.MainConfigOfPropertyValue;
import club.caihappy.pojo.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;

/**
 * Created by DELL on 2021/10/6
 **/
public class IOCTest_Property {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValue.class);

    @Test
    public void test1() {
        printBeans(applicationContext);
        Person person = (Person) applicationContext.getBean("person");
        System.out.println(person);
        Environment environment = applicationContext.getEnvironment();
        String property = environment.getProperty("person.nickName");
        System.out.println(property);
    }

    public void printBeans(ApplicationContext applicationContext) {
        //该方法用于获取 Spring 容器中定义的所有 JavaBean 的名称。
        String[] names = applicationContext.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
    }
}
